package com.data.basic.chapter05;

/** 使用头结点
 * Created by wangbing on 2018/5/5.
 */
public class Solution2 {//使用头结点
    public ListNode removeElements(ListNode head, int val) {
        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = head;

        ListNode prev = dummyHead;
        while(prev.next!=null){
            if (prev.next.val==val){
                ListNode delNode = prev.next;
                prev.next = delNode.next;
                delNode.next=null;
            }
            else{
                prev = prev.next;
            }
        }
        return dummyHead.next;
    }
}